package com.gxpt.app.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseFragment;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.view.Activity_city_select_2;
import com.gxpt.app.view.Activity_life_need_add;
import com.gxpt.app.view.Activity_login;
import com.gxpt.app.view.Activity_service_shop_add_edit;
import com.gxpt.app.view.Activity_services_search_list;
import com.utils.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dufeng on 2017/9/8.
 */

@ContentView(R.layout.fragment_home)
public class Fragment_home extends BaseFragment {



    Context context;

    Fragment                          fragment_home_life;
    Fragment                          fragment_home_service;
    Fragment                          fragment_home_center;
    Fragment                          currentFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private static final  String[] FRAGMENT_TAG = {"life","service","center"};
    List<Fragment> fragments = new ArrayList<Fragment>();


    @ViewInject(R.id.txt_currentCity)
    TextView txt_currentCity;

    @ViewInject(R.id.img_tocity)
    ImageView img_tocity;

    @ViewInject(R.id.search_txt)
    EditText search_txt;

    @ViewInject(R.id.mainListLayout)
    LinearLayout mainListLayout;

    @ViewInject(R.id.submenu0)
    LinearLayout submenu0;

    @ViewInject(R.id.submenu1)
    LinearLayout submenu1;

    @ViewInject(R.id.submenu2)
    LinearLayout submenu2;


    @ViewInject(R.id.line0)
    LinearLayout line0;

    @ViewInject(R.id.line1)
    LinearLayout line1;

    @ViewInject(R.id.line2)
    LinearLayout line2;

    @ViewInject(R.id.toXUQIU)
    Button toXUQIU;

    @ViewInject(R.id.toFUWU)
    Button toFUWU;

    int index = 0 ;//menu

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Logger.i("注册成功");
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
//        Logger.i(messageEvent.getMessage());
//        txt_currentCity.setText(1+messageEvent.getMessage());
        txt_currentCity.setText(AppUtils.get_area_sub_name(context));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden == false){
            AppUtils.isShowAreaSel(context);
        }
    }

    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        manager = getChildFragmentManager();
        View rootView = x.view().inject(this, inflater, container);
        initViews();
        initData();
        AppUtils.isShowAreaSel(context);
        return  rootView;
    }
    private void initViews() {
//        search_txt.setText(AppUtils.get_token(context));//临时

        if(!AppUtils.get_area_sub_name(context).isEmpty()){
            txt_currentCity.setText(AppUtils.get_area_sub_name(context));
        }

//        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(search_txt.getWindowToken(),0);

        fragment_home_life   = new Fragment_home_life();
        fragments.add(0,fragment_home_life);
        fragment_home_service   = new Fragment_home_service();
        fragments.add(0,fragment_home_service);
        fragment_home_center   = new Fragment_home_center();
        fragments.add(0,fragment_home_center);
        setDefaultFragment();

        search_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if ((actionId == 0 || actionId == 3) && event != null) {

                    if ((actionId == 0 ) && event != null) {
                        if(event.getAction() == KeyEvent.ACTION_DOWN){
                            Logger.i(" == "+search_txt.getText().toString());
                            InputMethodManager imm1 = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm1.hideSoftInputFromWindow(v.getWindowToken(), 0);//从控件所在的窗口中隐藏

//                            EventBus.getDefault().post(new MessageEvent(
//                                Constant.ACTION_to_search,
//                                search_txt.getText().toString(),
//                                "",
//                                ""));

                            Bundle b = new Bundle();
                            b.putString("keyword",search_txt.getText().toString());
                            Intent goSearch = new Intent();
                            goSearch.putExtra("DATA",b);
                            goSearch.setClass(context,Activity_services_search_list.class);
                            startActivity(goSearch);

                        }
                        return true;
                    }
                    return false;
            }


        });
    }
    private void initData(){

    }

    private void setDefaultFragment() {
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_content, fragment_home_life,FRAGMENT_TAG[index]).commit();
        currentFragment = fragment_home_life;
    }

    /**
     * 修改显示的内容 不会重新加载 *
     */
    public void switchContent(Fragment to) {
        if (!currentFragment.getTag().equals(to.getTag())) {

            transaction = manager.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(currentFragment).add(R.id.fragment_content, to,FRAGMENT_TAG[index]).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个

            }
            currentFragment = to;
        }
    }

    private void changeMenuView(){
        switch (index){
            case 0:
                line0.setBackgroundColor(getResources().getColor(R.color.red));
                line1.setBackgroundColor(getResources().getColor(R.color.white));
                line2.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case 1:
                line0.setBackgroundColor(getResources().getColor(R.color.white));
                line1.setBackgroundColor(getResources().getColor(R.color.red));
                line2.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case 2:
                line0.setBackgroundColor(getResources().getColor(R.color.white));
                line1.setBackgroundColor(getResources().getColor(R.color.white));
                line2.setBackgroundColor(getResources().getColor(R.color.red));
                break;
        }
    }




    @Event(value={R.id.submenu0,R.id.submenu1,R.id.submenu2,R.id.txt_currentCity,R.id.toXUQIU,R.id.img_tocity,R.id.search_txt,R.id.toFUWU})
    private void getEvent(View view){
        switch(view.getId()){

            case R.id.submenu0:
                if(index != 0){
                    index = 0 ;
                    changeMenuView();
                    switchContent(fragment_home_life);
                }
                break;
            case R.id.submenu1:
                if(index != 1){
                    index = 1 ;
                    changeMenuView();
                    switchContent(fragment_home_service);
                }
                break;
            case R.id.submenu2:
                if(index != 2){
                    index = 2 ;
                    changeMenuView();
                    switchContent(fragment_home_center);
                }
                break;
            case R.id.txt_currentCity:
            case R.id.img_tocity:
                Bundle b = new Bundle();
                b.putString("FORM_","Fragment_home");
                Intent gotype = new Intent();
                gotype.setClass(context, Activity_city_select_2.class);
                gotype.putExtra("DATA",b);
                startActivity(gotype);
                break;
            case R.id.toXUQIU:
                if(AppUtils.get_user_id(context)>0){
                    Intent gotAddneed = new Intent();
                    gotAddneed.setClass(context, Activity_life_need_add.class);
                    startActivity(gotAddneed);
                }else {
                    Intent go = new Intent();
                    go.setClass(context,Activity_login.class);
                    startActivity(go);
                }

                break;
            case R.id.toFUWU:
                if(AppUtils.get_user_id(context)>0){
                    Intent goViews = new Intent();
                    goViews.setClass(context, Activity_service_shop_add_edit.class);
                    startActivity(goViews);
                }else {
                    Intent go = new Intent();
                    go.setClass(context,Activity_login.class);
                    startActivity(go);
                }

                break;

        }
    }

}
